//
//  ShopVC.swift
//  AliBaiChuan
//
//  Created by 郑东喜 on 2017/2/10.
//  Copyright © 2017年 郑东喜. All rights reserved.
//  购物车

import UIKit

class ShopVC: BaseWebView {
    
    //会话session
    fileprivate lazy var _alSession : ALBBSession = {
        let _se : ALBBSession = ALBBSession()
        return _se
    }()

    
    ///分享按钮
    lazy var shareBtn: UIButton = {
        let share : UIButton = UIButton.init(frame: CGRect.init(x: SW * 0.2, y: SH * 0.75, width: 50, height: 50))
        share.backgroundColor = .gray
        share.alpha = 0.55
        return share
    }()

    
    //分享视图
    lazy var shreView: YSJShareView = {
        var shareView : YSJShareView = YSJShareView()
        
        shareView = YSJShareView.init(frame: CGRect(x: 0, y: UIScreen.main.bounds.height - 80, width: UIScreen.main.bounds.width, height: 80))
        shareView._delegate = self
        shareView.addItem("QQ", withImage: UIImage(named: "s_QQ")!)
        shareView.addItem("QQ空间", withImage: UIImage(named: "s_QZone")!)
        shareView.addItem("微信朋友圈", withImage: UIImage(named: "wx_Fri")!)
        shareView.addItem("微信好友", withImage: UIImage(named: "wx_Zone")!)
        shareView.layer.cornerRadius = 5
        
        return shareView
    }()
    
    //设置未登陆视图
    lazy var logBtn: UIButton = {
        let btn : UIButton = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 100, height: 100))
        
        btn.setTitle("登陆", for: .normal)
        btn.setTitleColor(UIColor.black, for: .normal)
        btn.addTarget(self, action: #selector(jumpTologin), for: .touchUpInside)
        return btn
    }()
    
    //检测是否授权登陆
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        prepareUI()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = "购物车"
    }
}

extension ShopVC {
    // MARK:- 加载购物车页面
    //检测是否登陆
    fileprivate func prepareUI() -> Void {
        
        let _isLogin = _alSession.isLogin()
        
        if _isLogin == true {
            
            self.weView.isHidden = false
            self.logBtn.isHidden = true
            
            let page = AlibcTradePageFactory.myCartsPage()
            
            let showParams : AlibcTradeShowParams = AlibcTradeShowParams()
            showParams.openType = .H5
            showParams.isNeedPush = true
            showParams.openType = ALiOpenType.H5
            
            AlibcTradeSDK.sharedInstance().tradeService().show(showParams.isNeedPush ? self.navigationController! : self, webView: self.weView, page: page, showParams: showParams, taoKeParams: nil, trackParam: nil, tradeProcessSuccessCallback: { (result) in
                
            }) { (error) in
                
            }
            
        } else {
            print("\((#file as NSString).lastPathComponent):(\(#line))\n")
            //登陆按钮
            self.weView.isHidden = true
            self.logBtn.isHidden = false
            view.addSubview(self.logBtn)
            logBtn.center = self.view.center
        }
    }
}

// MARK:- 未登陆界面
extension ShopVC {
    @objc fileprivate func jumpTologin() -> Void {
        GetUserModel.shared.getUData { (name, oId, SID, headUrl, _) in
            let _data = "用户名: \(name)" + "\n 头像地址：\(headUrl)" + "\n openID : \(oId)" + "\n openSID : \(SID))"
            showWithAlert(data: _data)
        }
    }
}

// MARK:- UIWebViewDelegate
extension ShopVC {
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        let urlString = request.url?.absoluteString
        print("\((#file as NSString).lastPathComponent):(\(#line))\n",urlString as Any)
        
        if navigationType == .linkClicked {
            self.navigationItem.rightBarButtonItem?.isEnabled = false
            
            self.hidesBottomBarWhenPushed = true
            self.openURL(urlStr: urlString)
            self.hidesBottomBarWhenPushed = false
        }
        
        return true
    }
}


extension ShopVC {
    fileprivate func openURL(urlStr : String?) -> Void {
        
        if let strVal = urlStr {
            print(strVal)
            let page = AlibcTradePageFactory.page(urlStr!)
            let service = AlibcTradeSDK.sharedInstance().tradeService()
            
            let showParams : AlibcTradeShowParams = AlibcTradeShowParams()
            showParams.openType = .H5
            showParams.isNeedPush = true
            
            ///加入词句调用天猫
            service?.show(showParams.isNeedPush ? self.navigationController! : self,
                          page: page,
                          showParams:showParams,
                          
                          taoKeParams: nil,
                          trackParam: ["track_key":"track_value"], tradeProcessSuccessCallback: { (result) in
                            print("\((#file as NSString).lastPathComponent):(\(#line))",result?.payResult?.paySuccessOrders as Any)
                            
                            
            }, tradeProcessFailedCallback: { (error) in
                
            })
        }else {
            print("str is nil")
        }
        
    }
}

// MARK:- 分享视图
extension ShopVC : YSJShareViewDelegate {
    internal func shareBtnClick(_ index: Int) {
        
    }

    @objc fileprivate func shareSEL() -> Void {
        
    }
}
